﻿using System;
using System.Collections.Generic;
using System.Text;

namespace mstdump_Analyzer
{
    class Device
    {
        //DevID, RevID addressess:
        //devid_addr = "0xf0014.0"
        //devid_addr_size = "16"

        //revid_addr = "0xf0014.16"
        //revid_addr_size = "8"

        //MLNX_Device("Hermon",  hex(0x190), hex(0xa0), -1, "ConnectX", 2, "HCA"),                       
        //MLNX_Device("HermonB0",hex(0x190), hex(0xb0), -1, "ConnectX2", 2, "HCA"),
        //MLNX_Device("Kfir",    hex(0x1f5), -1,  -1, "ConnectX3", 2, "HCA"),
        //MLNX_Device("Golan",   hex(0x1ff), -1,  -1, "ConnectIB", 2, "HCA")
        //MLNX_Device("Dolev",   hex(0x17d4), -1, -1, "BridgeX", 0, "BRIDGE"),                    
        //MLNX_Device("Shaldag", hex(0x1b3), -1,  -1, "InfiniscaleIV", 36, "SWITCH"),
        //MLNX_Device("Baz",     hex(0x245), -1,  -1, "SwitchX", 64, "SWITCH"),

        private string deviceName = "";
        private string deviceType = "";
        private string deviceFamily = "";
        private string numOfPorts = "0";
        private string psid = "N/A";

        private DeviceFwVerInterface devFwVer = null;
        //
        //----------------------------------------------------
        //
        public void identifyDevice(Dictionary<string, string> mstdumpDic)
        {
            // get devid_addr
            switch (Methods.GetValueFromOffset(Methods.SafeDicGetter(mstdumpDic,"f0014"), 0, 16).ToLower())
            {
                case "245": // "Baz", "SwitchX", 36, "SWITCH"
                    deviceName = "Baz";
                    deviceFamily = "SwitchX";
                    deviceType = "SWITCH";
                    numOfPorts = Methods.Hex2decString(Methods.GetValueFromOffset(Methods.SafeDicGetter(mstdumpDic,"6c8f0"), 0, 8).ToLower());
                    devFwVer = new SwitchFwVer();
                    // get the psid: 67a28.0 4, 67a2c.0 4, 67a30.0 4, 67a34.0 4
                    // ASCII...
                    psid = Methods.HexToAscii(Methods.GetValueFromOffset(Methods.SafeDicGetter(mstdumpDic,"67a28"), 24, 8).ToLower());
                    psid += Methods.HexToAscii(Methods.GetValueFromOffset(Methods.SafeDicGetter(mstdumpDic,"67a28"), 16, 8).ToLower());
                    psid += Methods.HexToAscii(Methods.GetValueFromOffset(Methods.SafeDicGetter(mstdumpDic,"67a28"), 8, 8).ToLower());
                    psid += Methods.HexToAscii(Methods.GetValueFromOffset(Methods.SafeDicGetter(mstdumpDic,"67a28"), 0, 8).ToLower());

                    psid += Methods.HexToAscii(Methods.GetValueFromOffset(Methods.SafeDicGetter(mstdumpDic,"67a2c"), 24, 8).ToLower());
                    psid += Methods.HexToAscii(Methods.GetValueFromOffset(Methods.SafeDicGetter(mstdumpDic,"67a2c"), 16, 8).ToLower());
                    psid += Methods.HexToAscii(Methods.GetValueFromOffset(Methods.SafeDicGetter(mstdumpDic,"67a2c"), 8, 8).ToLower());
                    psid += Methods.HexToAscii(Methods.GetValueFromOffset(Methods.SafeDicGetter(mstdumpDic,"67a2c"), 0, 8).ToLower());

                    psid += Methods.HexToAscii(Methods.GetValueFromOffset(Methods.SafeDicGetter(mstdumpDic,"67a30"), 24, 8).ToLower());
                    psid += Methods.HexToAscii(Methods.GetValueFromOffset(Methods.SafeDicGetter(mstdumpDic,"67a30"), 16, 8).ToLower());
                    psid += Methods.HexToAscii(Methods.GetValueFromOffset(Methods.SafeDicGetter(mstdumpDic,"67a30"), 8, 8).ToLower());
                    psid += Methods.HexToAscii(Methods.GetValueFromOffset(Methods.SafeDicGetter(mstdumpDic,"67a30"), 0, 8).ToLower());

                    psid += Methods.HexToAscii(Methods.GetValueFromOffset(Methods.SafeDicGetter(mstdumpDic,"67a34"), 24, 8).ToLower());
                    psid += Methods.HexToAscii(Methods.GetValueFromOffset(Methods.SafeDicGetter(mstdumpDic,"67a34"), 16, 8).ToLower());
                    psid += Methods.HexToAscii(Methods.GetValueFromOffset(Methods.SafeDicGetter(mstdumpDic,"67a34"), 8, 8).ToLower());
                    psid += Methods.HexToAscii(Methods.GetValueFromOffset(Methods.SafeDicGetter(mstdumpDic,"67a34"), 0, 8).ToLower());
                    break;
                case "1b3": // "Shaldag", "InfiniscaleIV", 36, "SWITCH"
                    deviceName = "Shaldag";
                    deviceFamily = "InfiniscaleIV";
                    deviceType = "SWITCH";
                    numOfPorts = Methods.Hex2decString(Methods.GetValueFromOffset(Methods.SafeDicGetter(mstdumpDic,"672ec"), 0, 8).ToLower());
                    devFwVer = new SwitchFwVer();
                    break;
                case "17d4": //"Dolev", "BridgeX", 0, "BRIDGE"
                    deviceName = "Dolev";
                    deviceFamily = "BridgeX";
                    deviceType = "BRIDGE";
                    numOfPorts = Methods.Hex2decString(Methods.GetValueFromOffset(Methods.SafeDicGetter(mstdumpDic,"6086c"), 0, 2).ToLower());
                    devFwVer = new SwitchFwVer();
                    break;
                case "1ff": // "Golan", "ConnectIB", 2, "HCA"
                    deviceName = "Golan";
                    deviceFamily = "ConnectIB";
                    deviceType = "HCA";
                    numOfPorts = Methods.Hex2decString(Methods.GetValueFromOffset(Methods.SafeDicGetter(mstdumpDic,"ee80"), 0, 8).ToLower());
                    devFwVer = new CardFwVer();
                    break;
                case "1f5": // "Kfir", "ConnectX3", 2, "HCA"
                    deviceName = "Kfir";
                    deviceFamily = "ConnectX3";
                    deviceType = "HCA";
                    numOfPorts = Methods.Hex2decString(Methods.GetValueFromOffset(Methods.SafeDicGetter(mstdumpDic,"1f3a0"), 0, 2).ToLower());
                    devFwVer = new CardFwVer();
                    break;
                case "190": // "Hermon" & "HermonB0"
                    // get revid_addr
                    switch (Methods.GetValueFromOffset(Methods.SafeDicGetter(mstdumpDic, "f0014"), 16, 8).ToLower())
                    {
                        case "a0": // "Hermon", "ConnectX", 2, "HCA"
                            deviceName = "Hermon";
                            deviceFamily = "ConnectX";
                            deviceType = "HCA";
                            numOfPorts = Methods.Hex2decString(Methods.GetValueFromOffset(Methods.SafeDicGetter(mstdumpDic,"1f3a0"), 0, 2).ToLower());
                            devFwVer = new CardFwVer();
                            break;
                        case "b0": // "HermonB0", "ConnectX2", 2, "HCA"
                            deviceName = "HermonB0";
                            deviceFamily = "ConnectX2";
                            deviceType = "HCA";
                            numOfPorts = Methods.Hex2decString(Methods.GetValueFromOffset(Methods.SafeDicGetter(mstdumpDic,"1f3a0"), 0, 2).ToLower());
                            devFwVer = new CardFwVer();
                            break;
                        default: // unknown device
                            deviceName = "unknown";
                            deviceFamily = "unknown";
                            deviceType = "unknown";
                            break;
                    }
                    break;
                default: // unknown device
                    deviceName = "unknown";
                    deviceFamily = "unknown";
                    deviceType = "unknown";
                    break;
            }//end of switch

            if (devFwVer != null)
            {
                devFwVer.identifyFW(mstdumpDic);
            }
        } // end of identifyDevice
        //
        //----------------------------------------------------
        //
        public string getInfo()
        {
            return "Device name: " + deviceName
                + "\r\nDevice type: " + deviceType
                + "\r\nDevice family: " + deviceFamily
                + "\r\nNumber of ports: " + numOfPorts
                + "\r\nPSID: " + psid
                + "\r\n\r\n" + devFwVer.getFwVer();
        }
        //
        //----------------------------------------------------
        //
    }
}
